<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>LINUX 初体验(二) | 小小错题本</title>
    <meta name="description" content="本文主要是对linux下的shell编程相关知识做一下总结。

1.什么是SHELL？什么是SHELL脚本？

&gt; Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。

&gt; Shell 是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。

&gt; Shell 脚本（shell s ...">
    <meta name="generator" content="VuePress 1.3.0">
    
    
    <link rel="preload" href="/wrong_question_static_page/assets/css/0.styles.f30cb765.css" as="style"><link rel="preload" href="/wrong_question_static_page/assets/js/app.9022c753.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/4.8c1e4aa5.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/5.33f70f09.js" as="script"><link rel="preload" href="/wrong_question_static_page/assets/js/12.12a9963e.js" as="script"><link rel="prefetch" href="/wrong_question_static_page/assets/js/1.ec18c3a1.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/10.697d33d5.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/11.488795fa.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/13.9d026eb1.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/14.c44f0e4a.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/15.a258b8bb.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/16.92328039.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/17.c4f16e4e.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/18.4dfa396f.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/19.edba2346.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/20.082608a3.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/21.5ae0ad82.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/22.89457782.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/23.53bf9e28.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/24.ae06dfc6.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/25.1d7d04a4.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/6.af07a62f.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/7.2c342931.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/8.2545b8d2.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/9.0eaa1c27.js"><link rel="prefetch" href="/wrong_question_static_page/assets/js/vuejs-paginate.c4011005.js">
    <link rel="stylesheet" href="/wrong_question_static_page/assets/css/0.styles.f30cb765.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div id="vuepress-theme-blog__global-layout"><section id="header-wrapper"><header id="header"><div class="header-wrapper"><div class="title"><a href="/wrong_question_static_page/" class="nav-link home-link">小小错题本 </a></div> <div class="header-right-wrap"><ul class="nav"><li class="nav-item"><a href="/wrong_question_static_page/tag/" class="nav-link">分类</a></li></ul> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <!----></div></div></header></section> <div id="mobile-header"><div class="mobile-header-bar"><div class="mobile-header-title"><a href="/wrong_question_static_page/" class="nav-link mobile-home-link">小小错题本 </a> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg></div> <div class="mobile-menu-wrapper"><hr class="menu-divider"> <ul class="mobile-nav"><li class="mobile-nav-item"><a href="/wrong_question_static_page/tag/" class="nav-link">分类</a></li> <li class="mobile-nav-item"><!----></li></ul></div></div></div> <div class="content-wrapper"><div id="vuepress-theme-blog__post-layout"><article itemscope="itemscope" itemtype="https://schema.org/BlogPosting" class="vuepress-blog-theme-content"><header><h1 itemprop="name headline" class="post-title">
        LINUX 初体验(二)
      </h1> <div class="post-meta"><!----> <div class="post-meta-date"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-clock"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg> <time pubdate itemprop="datePublished" datetime="2018-08-19T12:00:38.000Z">
      Sun Aug 19 2018
    </time></div> <ul itemprop="keywords" class="post-meta-tags"><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/linux" data-v-2ac33c20> linux </a></li><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/后台" data-v-2ac33c20> 后台 </a></li><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/服务器" data-v-2ac33c20> 服务器 </a></li><li class="post-tag" data-v-2ac33c20><a href="/wrong_question_static_page/tag/shell" data-v-2ac33c20> shell </a></li></ul></div></header> <div itemprop="articleBody" class="content__default"><blockquote><p>本文主要是对linux下的shell编程相关知识做一下总结。</p></blockquote> <h2 id="_1-什么是shell？什么是shell脚本？"><a href="#_1-什么是shell？什么是shell脚本？" class="header-anchor">#</a> 1.什么是SHELL？什么是SHELL脚本？</h2> <blockquote><p>Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。</p></blockquote> <blockquote><p>Shell 是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。</p></blockquote> <blockquote><p>Shell 脚本（shell script），是一种为 shell 编写的脚本程序。业界所说的 shell 通常都是指 shell 脚本，但读者朋友要知道，shell 和 shell script 是两个不同的概念。由于习惯的原因，简洁起见，本文出现的 &quot;shell编程&quot; 都是指 shell 脚本编程，不是指开发 shell 自身。</p></blockquote> <p><em><strong>简单来说，shell和shell脚本就是雷锋与雷峰塔的区别，听着都与雷锋有关而已。</strong></em></p> <h2 id="_2-shell脚本"><a href="#_2-shell脚本" class="header-anchor">#</a> 2.shell脚本</h2> <ul><li><p>shell脚本就是将一些linux操作命令集成为“.sh”脚本</p></li> <li><p>shell脚本第一行一般如下：</p> <div class="language-bash extra-class"><pre class="language-bash"><code> <span class="token comment">#!/bin/bash</span>
</code></pre></div></li> <li><p>shell脚本运行的方法：</p> <p>1.作为解释器参数</p> <div class="language-bash extra-class"><pre class="language-bash"><code> <span class="token function">bash</span> ./text.sh
</code></pre></div><p>2.作为可执行程序</p> <div class="language-bash extra-class"><pre class="language-bash"><code>./text.sh
</code></pre></div><p><em><strong>注意，一定要写成 ./test.sh，而不是 test.sh，运行其它二进制的程序也一样，直接写 test.sh，linux 系统会去 PATH 里寻找有没有叫 test.sh 的，而只有 /bin, /sbin, /usr/bin，/usr/sbin 等在 PATH 里，你的当前目录通常不在 PATH 里，所以写成 test.sh 是会找不到命令的，要用 ./test.sh 告诉系统说，就在当前目录找。</strong></em></p></li></ul> <h2 id="_3-shell编程注意事项："><a href="#_3-shell编程注意事项：" class="header-anchor">#</a> 3.shell编程注意事项：</h2> <h3 id="_1-变量-字符串"><a href="#_1-变量-字符串" class="header-anchor">#</a> 1. 变量&amp;&amp;字符串</h3> <ul><li>字符串变量定义时，单引号里面不能有其他变量拼接，会原样输出；在双引号里面可以包含其他变量引用后输出。</li> <li>定义变量时，变量名不加美元符号$,使用变量需要在前边加 <em><strong>$num</strong></em> 或者写成 <em><strong>${num}</strong></em>  。</li> <li>变量重新赋值不属于使用变量，不需要加$ 。<div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">your_name</span><span class="token operator">=</span><span class="token string">&quot;kw&quot;</span>                  <span class="token comment">#定义变量</span>
<span class="token builtin class-name">echo</span> <span class="token variable">$your_name</span>                    <span class="token comment">#使用变量</span>
<span class="token assign-left variable">your_name</span><span class="token operator">=</span><span class="token string">&quot;tiantian&quot;</span>               <span class="token comment">#重新赋值</span>
<span class="token builtin class-name">echo</span> your name is <span class="token variable">$your_name</span> <span class="token operator">!</span>     <span class="token comment">#使用变量</span>
<span class="token builtin class-name">echo</span> your name is <span class="token variable">${your_name}</span> <span class="token operator">!</span>   <span class="token comment">#使用变量</span>
</code></pre></div></li> <li>字符串拼接直接将变量“放置”在对于位置即可。
<blockquote><p>变量相关其他知识可以参考 <a href="http://www.runoob.com/linux/linux-shell-variable.html" target="_blank" rel="noopener noreferrer">菜鸟教程<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 相关章节。</p></blockquote></li></ul> <h3 id="_2-参数传递"><a href="#_2-参数传递" class="header-anchor">#</a> 2.参数传递</h3> <p><em><strong>shell执行机制本身就比较&quot;傻&quot;，它会从上到下一条条命令执行。假如没有做容错处理，执行错误了，也不会停止，会继续执行下面的命令直到脚本底部</strong></em></p> <p>假如测试脚本 file.sh 如下：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;Shell 传递参数实例！&quot;</span><span class="token punctuation">;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;执行的文件名：<span class="token variable">$0</span>&quot;</span><span class="token punctuation">;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第一个参数为：<span class="token variable">$1</span>&quot;</span><span class="token punctuation">;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第二个参数为：<span class="token variable">$2</span>&quot;</span><span class="token punctuation">;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第三个参数为：<span class="token variable">$3</span>&quot;</span><span class="token punctuation">;</span>
</code></pre></div><p>我们调用执行他：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">chmod</span> +x file.sh <span class="token comment">#给文件file.sh添加自执行权限</span>
./file.sh <span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span>  <span class="token comment">#调用脚本并传入参数</span>
</code></pre></div><p>执行结果：</p> <div class="language-text extra-class"><pre class="language-text"><code>Shell 传递参数实例！
执行的文件名：./file.sh
第一个参数为：1
第二个参数为：2
第三个参数为：3
</code></pre></div><blockquote><p>shell的脚本调用传参必须严格遵守传参顺序</p></blockquote> <h2 id="_4-shell数组"><a href="#_4-shell数组" class="header-anchor">#</a> 4.shell数组</h2> <p>1.定义方法：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">arr</span><span class="token operator">=</span><span class="token punctuation">(</span>A B <span class="token string">&quot;C&quot;</span> D<span class="token punctuation">)</span>
</code></pre></div><p>2.注意点：</p> <ul><li>数组定义以 () 包裹。</li> <li>shell数组只支持一维数组。</li> <li>shell数组元素以空格分割</li> <li>数组元素获取方法和JScript相同。<div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">arr1</span><span class="token operator">=</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>   <span class="token comment">#获取数组第一个元素</span>
<span class="token assign-left variable">arrL</span><span class="token operator">=</span>arr<span class="token punctuation">[</span>@<span class="token punctuation">]</span>   <span class="token comment">#获取数组长度</span>
<span class="token assign-left variable">arrL</span><span class="token operator">=</span>arr<span class="token punctuation">[</span>*<span class="token punctuation">]</span>   <span class="token comment">#同上</span>
</code></pre></div></li></ul> <h2 id="_5-流程控制"><a href="#_5-流程控制" class="header-anchor">#</a> 5.流程控制</h2> <p><em><strong>shell里面的流程控制语句，一定要注意关键词的之后空格和缩进</strong></em></p> <ol><li>if</li></ol> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token keyword">if</span> condition
<span class="token keyword">then</span>
    command1
    command2
    <span class="token punctuation">..</span>.
    commandN
<span class="token keyword">fi</span>
</code></pre></div><ol start="2"><li>if-else</li></ol> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">num1</span><span class="token operator">=</span>$<span class="token punctuation">[</span><span class="token number">2</span>*3<span class="token punctuation">]</span>
<span class="token assign-left variable">num2</span><span class="token operator">=</span>$<span class="token punctuation">[</span><span class="token number">1</span>+5<span class="token punctuation">]</span>
<span class="token keyword">if</span> <span class="token builtin class-name">test</span> $<span class="token punctuation">[</span>num1<span class="token punctuation">]</span> -eq $<span class="token punctuation">[</span>num2<span class="token punctuation">]</span>
<span class="token keyword">then</span>
    <span class="token builtin class-name">echo</span> <span class="token string">'两个数字相等!'</span>
<span class="token keyword">else</span>
    <span class="token builtin class-name">echo</span> <span class="token string">'两个数字不相等!'</span>
<span class="token keyword">fi</span>
</code></pre></div><ol start="3"><li>for</li></ol> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token keyword">for</span> <span class="token for-or-select variable">var</span> <span class="token keyword">in</span> item1 item2 <span class="token punctuation">..</span>. itemN
<span class="token keyword">do</span>
    command1
    command2
    <span class="token punctuation">..</span>.
    commandN
<span class="token keyword">done</span>
</code></pre></div><h2 id="_6-函数"><a href="#_6-函数" class="header-anchor">#</a> 6.函数</h2> <p><em><strong>shell里面的函数定义与JScript一样,不过要注意缩进，调用时如果不需要传参，不跟()</strong></em></p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>

<span class="token function-name function">funWithReturn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token builtin class-name">echo</span> <span class="token string">&quot;这个函数会对输入的两个数字进行相加运算...&quot;</span>
    <span class="token builtin class-name">echo</span> <span class="token string">&quot;输入第一个数字: &quot;</span>
    <span class="token builtin class-name">read</span> aNum
    <span class="token builtin class-name">echo</span> <span class="token string">&quot;输入第二个数字: &quot;</span>
    <span class="token builtin class-name">read</span> anotherNum
    <span class="token builtin class-name">echo</span> <span class="token string">&quot;两个数字分别为 <span class="token variable">$aNum</span> 和 <span class="token variable">$anotherNum</span> !&quot;</span>
    <span class="token builtin class-name">return</span> <span class="token variable"><span class="token variable">$((</span>$aNum<span class="token operator">+</span>$anotherNum<span class="token variable">))</span></span>
<span class="token punctuation">}</span>
funWithReturn  <span class="token comment">#函数调用</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;输入的两个数字之和为 <span class="token variable">$?</span> !&quot;</span>
</code></pre></div><p>函数传参方法：</p> <p>类似脚本运行传参,参数获取从$1开始，代表第一个参数。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function-name function">funWithReturn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
 <span class="token builtin class-name">echo</span> <span class="token string">&quot;第一个参数为：<span class="token variable">$1</span>&quot;</span><span class="token punctuation">;</span>
 <span class="token builtin class-name">echo</span> <span class="token string">&quot;第二个参数为：<span class="token variable">$2</span>&quot;</span><span class="token punctuation">;</span>
 <span class="token builtin class-name">echo</span> <span class="token string">&quot;第三个参数为：<span class="token variable">$3</span>&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
funWithReturn  a b c    <span class="token comment">#函数传参调用</span>

<span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span>

第一个参数为：a
第一个参数为：b
第一个参数为：c
</code></pre></div><h2 id="_7-shell-输入-输出重定向"><a href="#_7-shell-输入-输出重定向" class="header-anchor">#</a> 7.Shell 输入/输出重定向</h2> <blockquote><p>大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端。一个命令通常从一个叫标准输入的地方读取输入，默认情况下，这恰好是你的终端。同样，一个命令通常将其输出写入到标准输出，默认情况下，这也是你的终端。</p></blockquote> <p>用的较多的几个：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">command</span> <span class="token number">1</span> <span class="token operator">&gt;</span> file1.log   <span class="token comment">#将运行结果输入到file1.log文件中。</span>
<span class="token builtin class-name">command</span> <span class="token number">2</span> <span class="token operator">&gt;&gt;</span> <span class="token function">file</span>       <span class="token comment">#将运行结果输入追加到file1.log文件中。不删除以前的。</span>
<span class="token builtin class-name">command</span> <span class="token operator">&gt;</span> /dev/null     <span class="token comment">#/dev/null 是一个特殊的文件，写入到它的内容都会被丢弃；如果尝试从该文件读取内容，那么什么也读不到。但是 /dev/null 文件非常有用，将命令的输出重定向到它，会起到&quot;禁止输出&quot;的效果。</span>
</code></pre></div><h2 id="_8-shell-文件包含"><a href="#_8-shell-文件包含" class="header-anchor">#</a> 8.Shell 文件包含</h2> <blockquote><p>和其他语言一样，Shell 也可以包含外部脚本。这样可以很方便的封装一些公用的代码作为一个独立的文件。</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">.</span> filename

<span class="token comment"># 注意点号(.)和文件名中间有一空格.可以理解为JScript里面的import后可以直接使用filename中的变量、方法等.</span>
</code></pre></div><p>【未完待续】</p></div> <footer><!----> <hr> <!----></footer></article> <div class="sticker vuepress-toc"><div class="vuepress-toc-item vuepress-toc-h2 active"><a href="#_1-什么是shell？什么是shell脚本？" title="1.什么是SHELL？什么是SHELL脚本？">1.什么是SHELL？什么是SHELL脚本？</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#_2-shell脚本" title="2.shell脚本">2.shell脚本</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#_3-shell编程注意事项：" title="3.shell编程注意事项：">3.shell编程注意事项：</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_1-变量-字符串" title="1. 变量&amp;&amp;字符串">1. 变量&amp;&amp;字符串</a></div><div class="vuepress-toc-item vuepress-toc-h3"><a href="#_2-参数传递" title="2.参数传递">2.参数传递</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#_4-shell数组" title="4.shell数组">4.shell数组</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#_5-流程控制" title="5.流程控制">5.流程控制</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#_6-函数" title="6.函数">6.函数</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#_7-shell-输入-输出重定向" title="7.Shell 输入/输出重定向">7.Shell 输入/输出重定向</a></div><div class="vuepress-toc-item vuepress-toc-h2"><a href="#_8-shell-文件包含" title="8.Shell 文件包含">8.Shell 文件包含</a></div></div></div></div> <footer class="footer" data-v-06e1b74b><div class="footer-left-wrap" data-v-06e1b74b><ul class="contact" data-v-06e1b74b></ul></div> <div class="footer-right-wrap" data-v-06e1b74b><ul class="copyright" data-v-06e1b74b></ul></div></footer></div><div class="global-ui"><!----></div></div>
    <script src="/wrong_question_static_page/assets/js/app.9022c753.js" defer></script><script src="/wrong_question_static_page/assets/js/4.8c1e4aa5.js" defer></script><script src="/wrong_question_static_page/assets/js/5.33f70f09.js" defer></script><script src="/wrong_question_static_page/assets/js/12.12a9963e.js" defer></script>
  </body>
</html>
